package com.tianyang.yukuaifuzs.business.trade.presenter;

import com.tianyang.yukuaifuzs.business.trade.bean.WithdrawRecordOutput;
import com.tianyang.yukuaifuzs.business.trade.contract.WithdrawRecordContract;
import com.tianyang.yukuaifuzs.business.trade.model.TradeModel;
import com.tianyang.yukuaifuzs.common.interfaces.CommonConfig;

import java.util.List;

import rx.Observer;
import rx.Subscription;
import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;
import rx.subscriptions.CompositeSubscription;

/**
 * Description：提现记录
 * Created by xyj on 2024/3/11
 */
public class WithdrawRecordPresenter implements WithdrawRecordContract.Presenter {

    private WithdrawRecordContract.View mView;

    private CompositeSubscription mSubscription;

    public WithdrawRecordPresenter(WithdrawRecordContract.View mView) {
        this.mView = mView;
        mSubscription = new CompositeSubscription();
    }

    @Override
    public void getWithdrawRecordList(int pageNumber) {
        Subscription subscription = TradeModel.getInstance().gettixianRecord(pageNumber)
                .subscribeOn(Schedulers.io())
                .unsubscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<List<WithdrawRecordOutput>>() {
                    @Override
                    public void onCompleted() {
                    }

                    @Override
                    public void onError(Throwable e) {
                        mView.showWithdrawRecordListView(null, pageNumber == CommonConfig.PAGE_NUM);
                        mView.showLoadMore(CommonConfig.LOADMOREFAIL);
                    }

                    @Override
                    public void onNext(List<WithdrawRecordOutput> outputs) {
                        mView.showWithdrawRecordListView(outputs, pageNumber == CommonConfig.PAGE_NUM);
                        if (outputs != null) {
                            if (outputs.size() < CommonConfig.PAGE_SIZE) {
                                mView.showLoadMore(CommonConfig.LOADMOREEND);
                            } else {
                                mView.showLoadMore(CommonConfig.LOADMORECOMPLETE);
                            }
                        } else {
                            mView.showLoadMore(CommonConfig.LOADMOREEND);
                        }
                    }
                });
        mSubscription.add(subscription);
    }

    @Override
    public void onDestroyPresenter() {
        if (mSubscription != null && !mSubscription.isUnsubscribed()) {
            mSubscription.unsubscribe();
            mSubscription = null;
        }
        if (mView != null) {
            mView = null;
        }
    }
}
